package 线程通信;

import java.util.Random;

public class Test {
    public static void main(String[] args) {
        int[] ints = new int[10000000];
        for (int i = 0; i < ints.length; i++) {
            ints[i] = new Random().nextInt(100);
        }
        long startTime = System.currentTimeMillis();
        System.out.println(Add.total(ints));
        long endTime = System.currentTimeMillis();
        System.out.println("time = " + (endTime - startTime));
//        System.out.println(Arrays.toString(ints));
    }


}

class Add {
    private static long sum1 = 0;
    private static long sum2 = 0;

    public static long total(int[] ints) {
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < ints.length; i += 2) {
                sum1 += ints[i];
            }
        });

        Thread thread2 = new Thread(() -> {
            for (int i = 1; i < ints.length; i += 2) {
                sum2 += ints[i];
            }
        });
        thread1.start();
        thread2.start();
        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return sum1 + sum2;

    }}
